<template>
  <div class="warpper-box sendSalary">
    <a-card title="订单信息">
      <detail-list :col="2">
        <detail-list-item term="订单名称">{{ orderInfo.orderName }}</detail-list-item>
        <detail-list-item term="订单企业">{{ orderInfo.enterpriseName }}</detail-list-item>
        <detail-list-item term="订单周期">{{ orderInfo.period }}</detail-list-item>
        <detail-list-item term="补缴服务费总额">{{ orderInfo.serviceAmoutBackfill }}</detail-list-item>
      </detail-list>
    </a-card>
    <a-card style="margin-top: 10px;">
      <div style="display: flex;justify-content: space-between;align-items: flex-start;">
        <span v-if="orderInfo.inType == 'info'">
          <form-create v-model="params.fApi" :rule="params.rule" :option="params.option" @btn-search-click="addLoad"
            @btn-reset-click="reset" />
        </span>
        <span style="padding: 5px 0 0 0;" v-if="orderInfo.inType == 'edit'">
          <a-button type="primary" @click="downloadTemplate">导入服务费模板下载</a-button>
          <a-button type="primary" @click="importService">导入服务费</a-button>
          <input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" ref="fileExcel"
            @change="importServiceHandle" style="display: none;" />
        </span>
      </div>

      <a-table :columns="table.columns" :data-source="table.loadData" :pagination="table.pagination" rowKey="detailId">
      </a-table>
      <div style="height: 60px;"></div>
    </a-card>
    <!-- 返回||提交 -->
    <a-card class="footBtn" hoverable :bordered="false">
      <a-button icon="logout" class="ant-btn-warning" @click="goBack">返回</a-button>
      <span v-if="orderInfo.inType == 'edit'">
        <a-button type="primary" icon="file-done" @click="submit" style="margin-left: 10px;" :disabled="table.loadData.length == 0">提交</a-button>
      </span>
    </a-card>
  </div>
</template>

<script>
import store from '@/store'
import {
  PARAMS_SALARY_PAY
} from "./form"
import {
  OPTION_BLOCK,
  OPTION_INLINE
} from "../base_form"
import {
  getTablePayDetailPage,
  serviceSubmit,
  importExcelData
} from "@/api/human/salaryPaySendSalary"
import { mapGetters, mapActions } from 'vuex'
import DetailList from '@/components/tools/DetailList'
const DetailListItem = DetailList.Item
export default {
  name: 'sendSalaryPay',
  components: {
    DetailList,
    DetailListItem
  },
  data() {
    return {
      params: {
        fApi: {},
        rule: PARAMS_SALARY_PAY,
        option: OPTION_INLINE,
      },
      table: {
        columns: [{
          title: '姓名',
          dataIndex: 'userName',
        },
        {
          title: '身份证',
          dataIndex: 'idCard',
        },
        {
          title: '服务费',
          dataIndex: 'serviceAmountBackfill',
        }],
        loadData: [],
        pagination: {
          current: 1,
          pageSize: 10,
          total: 0,
          showSizeChanger: true,
					showQuickJumper: true,
          pageSizeOptions: ["10", "20", "50", "100"],
          showTotal: total => `共有 ${total} 条数据`,
        }
      },
      pagination: { current: 1, pageSize: 10 },
      selectedRowKeys: [], // 选择的table数据id
      selectedRows: [], // 选择的table数据

      orderInfo: {}, // 页面携带的订单信息
    }
  },
  filters: {
    filterMore(value, num) {
      if (!value) return ''
      if (value.length > num) {
        return value.slice(0, num) + '...'
      }
      return value
    }
  },
  mounted() {
    window.addEventListener('keyup', this.keyDown);
  },
  destroyed() {
    window.removeEventListener('keyup', this.keyDown);
  },
  beforeCreate() {
    this.events_form = this.$form.createForm(this, {
      name: 'events_form'
    })
  },
  computed: {
    ...mapGetters(['userInfo']),
  },
  created() {

  },
  activated() {
    this.$nextTick(() => {
      this.orderInfo = this.$route.query;
      if (this.orderInfo.inType == 'info') {
        this.loadTableDetail(true);
      }
    })
  },
  methods: {
    // table复选框选择
    onSelectChange(selectedRowKeys, selectedRows) {
      // console.log('selectedRowKeys changed: ', selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
      this.selectedRows = selectedRows;
    },

    // 获取补缴详情明细列表
    async loadTableDetail(flag) {
      this.table.loadData = [];

      let params = this.params.fApi.formData();
      if (flag) {
        this.table.pagination.current = 1;
        this.table.pagination.pageSize = 10;
      }
      params.backfillId = this.orderInfo.backfillId;
      params.pageNum = this.table.pagination.current;
      params.pageSize = this.table.pagination.pageSize;
      const { code, data } = await getTablePayDetailPage(params);
      if (code === '1') {
        this.table.loadData = data.content;
      }
    },
    handleTableDetailChange(pagination) {
      this.table.pagination.current = pagination.current;
      this.table.pagination.pageSize = pagination.pageSize;
      this.loadTableDetail(false);
    },
    // 下载模板
    downloadTemplate() {
      // const blob = new Blob([res]);
      // const fileName = '发薪明细模板.xlsx';
      const elink = document.createElement('a'); // 创建a标签
      // elink.download = fileName;
      elink.style.display = 'none';
      elink.href = 'https://pro-lhyg.oss-cn-shenzhen.aliyuncs.com/down/%E5%AF%BC%E5%85%A5%E6%9C%8D%E5%8A%A1%E8%B4%B9%E8%A1%A5%E4%BA%A4%E6%A8%A1%E6%9D%BF.xlsx';
      document.body.appendChild(elink);
      elink.click(); // 点击下载
      URL.revokeObjectURL(elink.href); // 释放URL 对象
      document.body.removeChild(elink); // 释放标签
    },
    // 导入表单
    importService() {
      this.$refs.fileExcel.click();
    },
    importServiceHandle(e) {
      e.preventDefault();
      let _this = this;
      let file = e.target.files[0];
      if (file === undefined) {
        return;
      }
      if (file.name.indexOf('.xls') === -1) {
        _this.$message.error("只支持xlsx");
        e.target.value = "";
        return;
      }
      if (!(file.size / 1024 / 1024 < 20)) {
        _this.$message.error('模版大小不能超过 20MB!');
        e.target.value = "";
        return;
      }
      e.target.value = "";
      if (file) {
        let formData = new FormData();
        formData.append("orderId", _this.orderInfo.orderId);
        formData.append("file", file);
        importExcelData(formData).then((res) => {
          if (res.code == 1) {
            _this.importVisible = false;
            _this.$message.success('操作成功');
            _this.table.loadData = res.data;
          }
        })
      }
    },
    addLoad() {
      if (this.orderInfo.inType == 'info') {
        this.loadTableDetail(true);
      }
    },
    reset() {
      if (this.orderInfo.inType == 'info') {
        this.params.fApi.resetFields();
        this.loadTableDetail(true);
      }
    },
    goBack() {
      this.$router.back();
    },
    // 提交
    async submit() {
      let params = {
        orderId: this.orderInfo.orderId,
        dtoList: this.table.loadData
      }
      const { code } = await serviceSubmit(params)
      if (code == '1') {
        this.$message.success("操作成功");
        this.table.loadData = [];
      }
    }
  }
}
</script>
<style lang="less"></style>